#!/bin/bash

base_dir=$(pwd)
architecture=$(uname -m)

source $base_dir/user_config

echo "[INFO] 安装编译和运行依赖的组件：gcc make libaio libaio-devel perl zlib-devel gcc-gfortran expect tar"
yum install -y gcc make libaio libaio-devel perl zlib-devel gcc-gfortran expect tar net-tools  2>&1 > /dev/null
if [ $? -eq 0 ]; then
    echo "[INFO] yum-安装成功"
else
    echo "[ERROR] yum-安装失败，退出测试 netperf"
    exit 1
fi

# 关闭防火墙和 selinux、关闭交换分区
echo "[INFO] 关闭交换区分"
swapoff -a
echo "[INFO] 关闭防火墙 firewalld"
systemctl stop firewalld
systemctl disable firewalld


function fun_scp()
{
    /usr/bin/expect <<EOF
set timeout 10
spawn scp $1 ${USERNAME}@${IP}:/home
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$PASSWD\n" }
  }
expect "password" { send "$PASSWD\n" }
EOF
}

function fun_ssh()
{
    /usr/bin/expect <<EOF
set timeout 10
spawn ssh  ${USERNAME}@${IP} "$1"
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "$PASSWD\n" }
   }
expect "password" { send "$PASSWD\n" }
EOF
}

# 配置本机数据面IP
echo "[INFO] 配置本机网口 $Node01EthName IP: $Node01Ip"
if [ -n $Node01EthName ];
then
    /usr/sbin/ifconfig "$Node01EthName" "$Node01Ip"
fi

# 配置对端数据面IP
echo "[INFO] 配置对端网口 $Node02EthName IP: $Node02Ip"
if [ -n $Node02EthName ];
then
    echo "[INFO] 拷贝 $base_dir/netserver_stop.sh 脚本到对端机器"
    fun_scp  $base_dir/netserver_stop.sh

    echo "[INFO] 在对端机器执行以下命令：yum install -y net-tools; chmod +x /home/netserver_stop.sh; /usr/sbin/ifconfig $Node02EthName $Node02Ip"
    fun_ssh "yum install -y net-tools; chmod +x /home/netserver_stop.sh; /usr/sbin/ifconfig $Node02EthName $Node02Ip"
fi

if [ -d "$base_dir/netperf-netperf-2.7.0" ]; then
    echo "[INFO] 删除 netperf 源码目录，使用重新解压后的源码"
    rm -rf "$base_dir/netperf-netperf-2.7.0"
fi

echo "[INFO] 解压缩 netperf 源码包"
tar -zxf $base_dir/netperf-netperf-2.7.0.tar.gz
cd  $base_dir/netperf-netperf-2.7.0
make clean  > /dev/null

echo "[INFO] 编译 netperf"
if [ "$architecture" == 'x86_64' ]; then
    ./configure   > /dev/null
else
    ./configure --build=arm-linux  > /dev/null
fi
make CFLAGS=-fcommon -j > /dev/null && make install  > /dev/null


echo "[INFO] kill 对端 netserver 进程"
fun_ssh "/usr/bin/sh /home/netserver_stop.sh"

echo "[INFO] 拷贝 netserver 到对端机器"
fun_scp $base_dir/netperf-netperf-2.7.0/src/netserver

echo "[INFO] 在对端机器执行以下命令：swapoff -a;systemctl stop firewalld;chmod +x /home/netserver;nohup /home/netserver &"
fun_ssh "swapoff -a;systemctl stop firewalld;chmod +x /home/netserver;nohup /home/netserver &"

echo "[INFO] ping 对端数据面 IP：${Node02Ip}"
ping -c3 "${Node02Ip}" 1> /dev/null
if [ $? -ne 0 ]; then
    echo "[ERROR] ping ${Node02Ip} failed"
    exit 1
fi

echo "[INFO] ping 对端 netserver 服务"
$base_dir/netperf-netperf-2.7.0/src/netperf -H "${Node02Ip}" -t TCP_STREAM -l 1  1> /dev/null
if [ $? -ne 0 ]; then
	echo "[ERROR] netserver maybe not started"
	exit 1
fi

####### 开始测试 #######
#清理缓存
sync
echo 3 > /proc/sys/vm/drop_caches
sleep 3s

if [ -z "${log_file}" ]; then
    cur_time=`date +"%Y%m%d_%H%M%S"`
    log_dir="/home/log_${cur_time}_`uname -r`"
    mkdir -p "${log_dir}"
    declare log_file="${log_dir}"/netperf
fi

echo "[INFO] 开始测试 Unixbench"
for j in $(seq $netperf_run_counts);
do
    # TCP批量处理性能
    echo "[INFO] ----- 第$j轮 TCP 测试 -----"
    sleep 3s
    $base_dir/netperf-netperf-2.7.0/src/netperf -H $Node02Ip -t TCP_STREAM -l 120 > "${log_file}_${j}"

    # UDP批量传输性能
    echo "[INFO] ----- 第$j轮 UDP 测试 -----"
    sleep 3s
    $base_dir/netperf-netperf-2.7.0/src/netperf -H $Node02Ip -t UDP_STREAM -l 120 >> "${log_file}_${j}"

    # TCP_RR请求应答性能
    echo "[INFO] ----- 第$j轮 TCP_RR 测试 -----"
    sleep 3s
    $base_dir/netperf-netperf-2.7.0/src/netperf -H $Node02Ip -t TCP_RR -l 120 >> "${log_file}_${j}"

    # UDP_RR请求应答性能
    echo "[INFO] ----- 第$j轮 UDP_RR 测试 -----"
    sleep 3s
    $base_dir/netperf-netperf-2.7.0/src/netperf -H $Node02Ip -t UDP_RR -l 120 >> "${log_file}_${j}"

    # TCP_CRR请求应答性能
    echo "[INFO] ----- 第$j轮 TCP_CRR 测试 -----"
    sleep 3s
    $base_dir/netperf-netperf-2.7.0/src/netperf -H $Node02Ip -t TCP_CRR -l 120 >> "${log_file}_${j}"

    echo -e "\033[32m [INFO] `date` netperf 运行完成，结果存放在 ${log_file}_${j} \033[0m"
done

